package xin.baizhiedu.Util;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class DateTimeUtils {
    /**
     * 获取当天日期 年月日
     *
     * @return
     */
    public static String ToDay(int day) {
        LocalDate today = LocalDate.now();
        // 创建一个日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 使用格式化器格式化日期
        return today.minusDays(day).format(formatter);
    }

    /**
     * 获取当天日期周一
     *
     * @return
     */
    public static String ToMonDay() {
        LocalDate today = LocalDate.now();
        // 创建一个日期格式化器
        LocalDate mondayThisWeek = today.with(DayOfWeek.MONDAY); // 获取本周第一天，即周一
        // 使用格式化器格式化日期
        return mondayThisWeek.toString();
    }

    /**
     * 获取当天日期周日
     *
     * @return
     */
    public static String ToSunday() {
        LocalDate today = LocalDate.now();
        // 创建一个日期格式化器
        LocalDate mondayThisWeek = today.with(DayOfWeek.SUNDAY); // 获取本周第一天，即周一
        // 使用格式化器格式化日期
        return mondayThisWeek.toString();
    }

    /**
     * 获取当天日期  月初第一天
     *
     * @return
     */
    public static String firstDayOfMonth() {
        LocalDate firstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());  // 获取本月开始一天
        return firstDay.toString();
    }

    /**
     * 获取当天日期  月最后一天
     *
     * @return
     */
    public static String lastDayOfMonth() {
        LocalDate today = LocalDate.now();
        LocalDate lastDay = today.with(TemporalAdjusters.lastDayOfMonth());
        return lastDay.toString();
    }

    /**
     * 获取当前日期 季度第一天
     *
     * @return
     */
    public static String firstDaySeason() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前年份和月份
        YearMonth yearMonth = YearMonth.from(currentDate);

        // 计算当前季度的起始月份
        int month = (yearMonth.getMonthValue() - 1) / 3 * 3 + 1;
        YearMonth firstMonthOfQuarter = YearMonth.of(yearMonth.getYear(), month);

        // 获取季度第一天
        LocalDate firstDayOfQuarter = firstMonthOfQuarter.atDay(1);

        // 输出当前季度的第一天
        System.out.println("当前季度的第一天是: " + firstDayOfQuarter);
        return firstDayOfQuarter.toString();
    }

    /**
     * 获取当前日期 季度最后一天
     *
     * @return
     */
    public static String lastDaySeason() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 获取当前年份和月份
        YearMonth yearMonth = YearMonth.from(currentDate);

        // 计算当前季度的最后一个月
        int quarter = (yearMonth.getMonthValue() - 1) / 3 + 1;
        LocalDate lastDayOfQuarter;

        // 根据季度确定最后一个月
        switch (quarter) {
            case 1: // 第一季度
                lastDayOfQuarter = YearMonth.of(yearMonth.getYear(), 3).atEndOfMonth();
                break;
            case 2: // 第二季度
                lastDayOfQuarter = YearMonth.of(yearMonth.getYear(), 6).atEndOfMonth();
                break;
            case 3: // 第三季度
                lastDayOfQuarter = YearMonth.of(yearMonth.getYear(), 9).atEndOfMonth();
                break;
            case 4: // 第四季度
                lastDayOfQuarter = YearMonth.of(yearMonth.getYear(), 12).atEndOfMonth();
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + quarter);
        }

        // 输出当前季度的最后一天
        System.out.println("当前季度的最后一天是: " + lastDayOfQuarter);
        return lastDayOfQuarter.toString();
    }

    public static String firstDayYear() {
        // 获取当前年份
        int currentYear = LocalDate.now().getYear();
        // 创建当前年份的第一天（1月1日）
        LocalDate firstDayOfYear = LocalDate.of(currentYear, 1, 1);
        // 输出当前年份的第一天
        System.out.println("当前年份的第一天是: " + firstDayOfYear);
        return firstDayOfYear.toString();
    }

    public static String lastDayYear() {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 获取当前年份的最后一天
        LocalDate lastDayOfYear = currentDate.with(TemporalAdjusters.lastDayOfYear());

        // 输出当前年份的最后一天
        System.out.println("当前年份的最后一天是: " + lastDayOfYear);
        return lastDayOfYear.toString();
    }


    public static void main(String[] args) {
        firstDayYear();
        lastDayYear();
    }


}
